//
//  MYComposeView.swift
//  Sina
//
//  Created by 王亮龙1 on 16/5/29.
//  Copyright © 2016年 lianglong. All rights reserved.
//

import UIKit
import SnapKit

class MYComposeView: UIView {
    
    //定义一个VC的属性,用于接收mianVC,便于使用
    weak var target: UIViewController?
    //btn数组
    private lazy var buttons: [MYComposeBtn] = [MYComposeBtn]()
    
    
    override init(frame: CGRect) {
        //定义视图大小
        let rect = UIScreen.mainScreen().bounds
        super.init(frame: rect)
        backgroundColor = UIColor.redColor()
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    func show(targetVC target: UIViewController) {
        //把mainVC赋值给VC属性
        self.target = target
        //mainVC添加子视图(composeView)
        target.view.addSubview(self)
    }
    
    
    
    
    //将要移动到父控件时让btn开始动画
    override func willMoveToSuperview(newSuperview: UIView?) {
        super.willMoveToSuperview(superview)
        
        for (index, btn) in buttons.enumerate() {
            startAnimation(btn, index: index, offset: -350)
        }
    }
    
    
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        
        for (index, btn) in buttons.reverse().enumerate() {
            startAnimation(btn, index: index, offset: 350)
        }
        
    }
    
    
    
    //开始动画
    private func startAnimation(btn: MYComposeBtn, index: Int, offset: CGFloat) {
        
        UIView.animateWithDuration(0.8, delay: Double(index) * 0.02, usingSpringWithDamping: 0.6, initialSpringVelocity: 5, options: [], animations: {
            //改变btn的center
            btn.center = CGPoint(x: btn.center.x, y: btn.center.y + offset)
            }) { (_) in
                //
                if offset > 0{
                    self.removeFromSuperview()
                }
        }
        
    }
    
    
    //布局子控件
    private func setupUI() {
        
        addSubview(backImageView)
        addSubview(sloganImageView)
        
        //设置约束
        sloganImageView.snp_makeConstraints { (make) in
            make.top.equalTo(self.snp_top).offset(100)
            make.centerX.equalTo(self.snp_centerX)
        }
        
        //添加btn
        addChildBtns()
    }
    
    
    //添加btn
    private func addChildBtns() {
        
        let count = self.composeItems.count
        
        let width: CGFloat = 80
        let height: CGFloat = 120
        let margin: CGFloat = (UIScreenWidth - width * 3) / 4
        
        for i in 0..<count {
            
            let btn:MYComposeBtn = MYComposeBtn()
            //取模型,赋值
            let item = composeItems[i]
            btn.composeItem = item
            
            btn.addTarget(self, action: #selector(MYComposeView.btnClick(_:)), forControlEvents: .TouchUpInside)
            
            let row = i / 3
            let clo = i % 3
            let x = CGFloat(clo) * (margin + width) + margin
            //先让btn隐藏在屏幕下边
            let y = CGFloat(row) * (margin + height) + UIScreenHeight
            btn.frame = CGRect(x: x, y: y, width: width, height: height)
            
            self.addSubview(btn)
            
            self.buttons.append(btn)
            
        }
        
    }
    
    
    //btn 的点击方法
    @objc private func btnClick(btn: MYComposeBtn) {
        //print("我被点了")
        
        var scale: CGFloat = 0
        
        for button in buttons {
            if button == btn {
                scale = 2.0
            } else {
                scale = 0.2
            }
            
            
            UIView.animateWithDuration(0.25, animations: {
                //改变透明度,改变大小
                button.alpha = 0
                button.transform = CGAffineTransformMakeScale(scale, scale)
                }, completion: { (_) in
                   // print("👌")
            })
        }
        
        
        guard let target = btn.composeItem?.target else {
            return
        }
        
        //根据字符串获取类型
        let type = NSClassFromString(target) as! UIViewController.Type
        //实例化控制器
        let vc = type.init()
        let nav = UINavigationController(rootViewController: vc)
        //vc.navigationItem.title = btn.composeItem?.title
        
        //让mainVC跳转控制器
        self.target?.presentViewController(nav, animated: true, completion: { 
            self.removeFromSuperview()
        })
        
    }
    
    

    
    //懒加载子控件
    //高斯模糊-遮罩
    private var backImageView: UIImageView = UIImageView(image: UIImage.snapShotCurrentWindow().applyLightEffect())
    //logo视图控件
    private var sloganImageView: UIImageView = UIImageView(image: UIImage(named: "compose_slogan"))
    
    //btn 数组
    private lazy var composeItems: [MYComposeItem] = {
        
        var composeArr = [MYComposeItem]()
        let path = NSBundle.mainBundle().pathForResource("Compose.plist", ofType: nil)!
        let itemArr = NSArray(contentsOfFile: path) as! [[String : AnyObject]]
        
        for item in itemArr {
            
            let composeItem = MYComposeItem(dict: item)
            composeArr.append(composeItem)
        }
        
        
        return composeArr
    }()
    


}
